package testapp.client.animation;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.animation.client.AnimationScheduler;
import com.google.gwt.animation.client.AnimationScheduler.AnimationCallback;

public class CompositeAnimationFrame {
    private static CompositeAnimationFrame instance = new CompositeAnimationFrame();

    public static CompositeAnimationFrame getInstance() {
        return instance;
    }

    private List<AnimationCallbackPart> parts = new ArrayList<AnimationCallbackPart>();

    public CompositeAnimationFrame() {
        AnimationScheduler.get().requestAnimationFrame(new AnimationCallback() {
            @Override
            public void execute(double timestamp) {
                for (AnimationCallbackPart part : parts) {
                    part.execute();
                }

                AnimationScheduler.get().requestAnimationFrame(this);
            }
        });
    }

    public void addAnimationCallbackPart(AnimationCallbackPart part) {
        parts.add(part);
    }

    public void removeAnimationCallbackPart(AnimationCallbackPart part) {
        parts.remove(part);
    }
}
